import 'dart:convert';

import 'package:custompaint/bean/CurveLineBean.dart';
import 'package:custompaint/bean/FakeWidget.dart';
import 'package:custompaint/bean/TabBean.dart';
import 'package:custompaint/bean/WidgetType.dart';
import 'package:custompaint/util/Util.dart';
import 'package:custompaint/widget/dialog/SelectWidgetDialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:uuid/uuid.dart';

import 'state.dart';

class MainLogic extends GetxController {
  final MainState state = MainState();

  var pageController = PageController();
  Future<void> addWidget(TabBean tabBean) async {
    WidgetType? type = await showSelectWidgetDialog();
    if (type == null) {
      return;
    }
    switch (type) {
      case WidgetType.line:
        tabBean.list.add(FakeWidget(
            color: Colors.black.obs,
            strokeWidth: (3.0).obs,
            startX: (0.0).obs,
            startY: (0.0).obs,
            endX: (1.0).obs,
            endY: (1.0).obs,
            uuid: const Uuid().v4(),
            type: WidgetType.line,
            name: "".obs,
            strokeCap: StrokeCap.square.obs,
            curvePointList: <CurveLineBean>[].obs,
            circleRadius: (3.0).obs,
            style: PaintingStyle.fill.obs,
            rectLeft: (0.0).obs,
            rectTop: (0.0).obs,
            rectRight: (0.0).obs,
            rectBottom: (0.0).obs,
            angle: (0.0).obs));
      case WidgetType.curve:
        tabBean.list.add(FakeWidget(
            color: Colors.black.obs,
            strokeWidth: (3.0).obs,
            startX: (0.0).obs,
            startY: (0.0).obs,
            endX: (1.0).obs,
            endY: (1.0).obs,
            uuid: const Uuid().v4(),
            type: WidgetType.curve,
            name: "".obs,
            strokeCap: StrokeCap.square.obs,
            curvePointList: <CurveLineBean>[
              CurveLineBean(
                  startX: (0.2).obs,
                  endX: (0.2).obs,
                  startY: (0.5).obs,
                  endY: (0.5).obs),
              CurveLineBean(
                  startX: (0.6).obs,
                  endX: (0.6).obs,
                  startY: (1.0).obs,
                  endY: (1.0).obs),
            ].obs,
            circleRadius: (3.0).obs,
            style: PaintingStyle.fill.obs,
            rectLeft: (0.0).obs,
            rectTop: (0.0).obs,
            rectRight: (0.0).obs,
            rectBottom: (0.0).obs,
            angle: (0.0).obs));
      case WidgetType.circle:
        tabBean.list.add(FakeWidget(
            color: Colors.black.obs,
            strokeWidth: (3.0).obs,
            startX: (0.0).obs,
            startY: (0.0).obs,
            endX: (1.0).obs,
            endY: (1.0).obs,
            uuid: const Uuid().v4(),
            type: WidgetType.circle,
            name: "".obs,
            strokeCap: StrokeCap.square.obs,
            curvePointList: <CurveLineBean>[].obs,
            circleRadius: (50.0).obs,
            style: PaintingStyle.fill.obs,
            rectLeft: (0.0).obs,
            rectTop: (0.0).obs,
            rectRight: (0.0).obs,
            rectBottom: (0.0).obs,
            angle: (0.0).obs));
      case WidgetType.elliptic:
        tabBean.list.add(FakeWidget(
            color: Colors.black.obs,
            strokeWidth: (3.0).obs,
            startX: (0.0).obs,
            startY: (0.0).obs,
            endX: (1.0).obs,
            endY: (1.0).obs,
            uuid: const Uuid().v4(),
            type: WidgetType.elliptic,
            name: "".obs,
            strokeCap: StrokeCap.square.obs,
            curvePointList: <CurveLineBean>[].obs,
            circleRadius: (3.0).obs,
            style: PaintingStyle.fill.obs,
            rectLeft: (0.26).obs,
            rectTop: (0.09).obs,
            rectRight: (0.78).obs,
            rectBottom: (0.9).obs,
            angle: (0.0).obs));
      case WidgetType.nAngle:
        tabBean.list.add(FakeWidget(
            color: Colors.black.obs,
            strokeWidth: (3.0).obs,
            startX: (0.0).obs,
            startY: (0.0).obs,
            endX: (1.0).obs,
            endY: (1.0).obs,
            uuid: const Uuid().v4(),
            type: WidgetType.nAngle,
            name: "".obs,
            strokeCap: StrokeCap.square.obs,
            curvePointList: <CurveLineBean>[
              CurveLineBean(
                  startX: (0.2).obs,
                  endX: (0.2).obs,
                  startY: (0.0).obs,
                  endY: (0.0).obs),
              CurveLineBean(
                  startX: (0.6).obs,
                  endX: (0.6).obs,
                  startY: (0.0).obs,
                  endY: (0.0).obs),
              CurveLineBean(
                  startX: (0.3).obs,
                  endX: (0.4).obs,
                  startY: (0.0).obs,
                  endY: (0.0).obs),
            ].obs,
            circleRadius: (3.0).obs,
            style: PaintingStyle.fill.obs,
            rectLeft: (0.0).obs,
            rectTop: (0.0).obs,
            rectRight: (0.0).obs,
            rectBottom: (0.0).obs,
            angle: (0.0).obs));
    }
  }

  void refreshList() {
    if (state.pageIndex.value >= 0) {
      var l = state.frames[state.pageIndex.value].list
          .map((element) => element.clone())
          .toList();
      state.frames[state.pageIndex.value].list.assignAll(l);
    }
  }

  Future<void> export() async {
    String code2 = await Util.genCode(
        state.frames,
        state.backgroundColor.value.value,
        state.panelHeight.value,
        state.panelWidth.value);
    showCode(code2);
  }

  Future<void> addAnimation() async {
    String? text = await showInputDialog(const InputDecoration(
      hintText: "请输入桢名称，建议英文小写",
      border: InputBorder.none,
    ));
    if (text != null) {
      state.frames.add(TabBean(list: <FakeWidget>[].obs, animationsName: text));
      try {
        pageController.animateToPage(state.frames.length,
            duration: const Duration(milliseconds: 200), curve: Curves.linear);
      } catch (e) {}
    }
  }

  Future<void> inputJson() async {
    String? text = await showInputDialog(const InputDecoration(
      hintText: "请输入json格式",
      border: InputBorder.none,
    ));
    if (text != null) {
      var list = jsonDecode(text);
      if (list is List) {
        state.frames.value = list.map((e) {
          return TabBean.fromJson(e);
        }).toList();
      }
    }
  }

  void outputJson() {
    showCode(
        jsonEncode(state.frames.map((element) => element.toJson()).toList()));
  }
}
